#include "UserOperationDataModel.hpp"
#include "DecimalData.hpp"

#include <QEvent>
#include <QDialog>

UserOperationDataModel::UserOperationDataModel()
{
    m_unit = GlobalData::getInstance().getCurUnit();
    installEventFilter(this);
}

unsigned int UserOperationDataModel::nPorts(PortType portType) const
{
    unsigned int result;

    if (portType == PortType::In)
        result = m_unit.inputPins.size();
    else
        result = m_unit.outputPins.size();

    return result;
}

NodeDataType UserOperationDataModel::dataType(PortType, PortIndex) const
{
    return DecimalData().type();
}

std::shared_ptr<NodeData> UserOperationDataModel::outData(PortIndex port)
{
    if (port < 1)
        return std::make_shared<MyNodeData>();

    return std::make_shared<SimpleNodeData>();
}

void UserOperationDataModel::setInData(std::shared_ptr<NodeData> data, PortIndex portIndex)
{
}

bool UserOperationDataModel::portCaptionVisible(PortType portType, PortIndex portIndex) const
{
    Q_UNUSED(portType);
    Q_UNUSED(portIndex);
    return true;
}

QString UserOperationDataModel::caption() const
{
    return m_unit.name;
}

QString UserOperationDataModel::portCaption(PortType portType, PortIndex portIndex) const
{
    switch (portType) 
    {
    case PortType::In:
        return m_unit.inputPins[portIndex].name;
        break;

    case PortType::Out:
        return m_unit.outputPins[portIndex].name;

    default:
        break;
    }
    return QString();
}

QString UserOperationDataModel::name() const
{
    return m_unit.name;
}

bool UserOperationDataModel::eventFilter(QObject* object, QEvent* event)
{
    if (event->type() == QEvent::MouseButtonDblClick) {
        QDialog* dialog = new QDialog();
    }

    return false;
}

void UserOperationDataModel::compute()
{
}
